#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char start[30] = {0};
char end[30] = {0};
int turns = 10;

char mapf[10][23] = {};
char mapt[10][23] = {};
int maps = 6;

char stack[13][256] = {};

int try(int depth, char *from, char *to)
{
    char *p = strstr(stack[depth], from);
    if (!p)
        return 0;
    char *out = stack[depth + 1];
    char *q;
    for (q = stack[depth]; q < p; ++q)
    {
        *out = *q;
        ++out;
    }
    for (int i = 0; i < strlen(to); ++i)
    {
        *out = to[i];
        ++out;
    }
    for (int i = 0; i < strlen(from); ++i)
        ++q;
    while (*q)
    {
        *out = *q;
        ++q;
        ++out;
    }
    *out = 0;
    //printf("%3d %s", depth, stack[depth + 1]);
}

void dp(int depth)
{
    if (depth > turns)
        return;
    if (strcmp(stack[depth], end) == 0)
    {
        turns = depth;
        return;
    }
    for (int i = 0; i < maps; ++i)
    {
        if (try(depth, mapf[i], mapt[i]))
            dp(depth + 1);
    }
}

int main()
{
    scanf("%s%s", start, end);
    strcpy(stack[0], start);
    for (int i = 0; i < 6; ++i)
    {
        scanf("%s%s", mapf[i], mapt[i]);
        if (!mapf[i][0] || mapf[i][0] == '\n')
        {
            maps = i;
            break;
        }
    }
    dp(0);
    if (turns < 10)
        printf("%d", turns);
    else
        printf("NO ANSWER!");
    return 0;
}